package com.goal98.team.domain

import grails.test.*

class TeamTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testGetTeamAdmins() {

        mockDomain(Team, [])

        Team team = new Team(name:'Team', members:[])
        def playerControl = mockFor(Person)
        boolean isAdmin = false
        def id = 0

        playerControl.demand.isTeamAdmin(3..3){->
            isAdmin = !isAdmin
            return isAdmin
            }
        playerControl.demand.getId(0..3){->
            return id++
            }

        team.addToMembers(playerControl.createMock())
        team.addToMembers(playerControl.createMock())
        team.addToMembers(playerControl.createMock())

        def admins = team.teamAdmins

        assertEquals 2, admins.size()
        

    }
    
    void testConstraints(){
        def teamExist = new Team(name:'Team 1')
    	mockForConstraintsTests(Team, [teamExist])
    	
    	def teamFail = new Team()
    	assertFalse teamFail.validate()
        assertEquals 'nullable', teamFail.errors['name']

        def teamDuplicate = new Team(name:'Team 1')
        assertFalse teamDuplicate.validate()
        assertEquals 'unique', teamDuplicate.errors['name']

        def teamPass = new Team(name:'Team 2')
        assertTrue teamPass.validate()
    }

    void testToString(){
        def team = new Team(name:'TeamA')
        assertEquals 'TeamA', team.toString()
    }
}
